package org.chengn.level_middle.context1094;

import java.util.PriorityQueue;

public class Context1094 {
    public boolean carPooling(int[][] trips, int capacity) {

        PriorityQueue<int[]> queue = new PriorityQueue<>((o1, o2) -> {
            if (o1[0] != o2[0]) {
                return o1[0] - o2[0];
            }
            return o1[1] - o2[1];
        });

        for (int[] trip : trips) {
            int[] tmpUp = new int[2];
            tmpUp[0] = trip[1];
            tmpUp[1] = trip[0];
            queue.add(tmpUp);
            int[] tmpDown = new int[2];
            tmpDown[0] = trip[2];
            tmpDown[1] = -trip[0];
            queue.add(tmpDown);
        }

        int capMax = 0;
        while (!queue.isEmpty()) {
            int[] poll = queue.poll();
            capMax += poll[1];
            if (capMax > capacity) return false;
        }
        return true;



    }
}
